// 手机号脱敏
export function phoneNumbe(val : string) {
	return val.substr(0, 3) + '****' + val.substring(7);
}
// 定义手机号校验函数
export function validatePhoneNumber(phone : string) {
	// 手机号格式为1开头的11位数字
	const regExp = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/;

	if (regExp.test(phone)) {
		return true;
	} else {
		return false;
	}
}
// 节流
export const throttle = (fn : any, time : number) => {
	let timer = null;
	time = time || 1000;
	return function (...args) {
		if (timer) {
			return;
		}
		const _this = this;
		timer = setTimeout(() => {
			timer = null;
		}, time);
		fn.apply(_this, args);
	};
};

// 防抖
export const debounce = (fn : any, time : number) => {
	time = time || 200;
	// 定时器
	let timer = null;
	return function (...args) {
		const _this = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function () {
			timer = null;
			fn.apply(_this, args);
		}, time);
	};
};
// 保留两位小数
export const formater = (data : any) => {
	if (!data) return '0.00'
	data = parseFloat(data).toFixed(2)
	const intPart = Math.trunc(data)
	const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
	let floatPart = '.00'
	const newArr = data.toString().split('.')
	if (newArr.length === 2) {
		floatPart = newArr[1].toString()
		return intPartFormat + '.' + floatPart
	}
	return intPartFormat + floatPart
}
/**
 * 判断字符是否为空
 * str：字符串
 */
export const isNullorEmpty = (str : any) => {
	return (str == null || str == "" || str == undefined || str == "undefined") && (str != 0 || str != "0") ? true : false;
}